<?php
/**
 * 主题管理
 * User: ws
 * Date: 2018/11/3
 * Time: 12:52
 */

namespace app\admin\controller;
use app\admin\model\Theme as TModeL;
use think\Db;
class Theme     extends Init
{
    function index(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $where['name'] = array('like','%'.trim($key).'%');
            $list = TModeL::order('theme_id desc')->where($where)->paginate(input('limit'))->toArray();
            foreach ($list['data'] as $key=>$itme){
                $list['data'][$key]['img'] = str_replace("/theme",config('aliyun_oss.url')."theme",$itme['img']);
            }
            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        return view('list');
    }

    function add(){
        if(request()->isPost()){
            $data = $this->request->post();
            if(isset($data['img'])==false){  return json(array('code'=>0,'msg'=>'添加失败,请上图焦点图'));}
            if(isset($data['state'])==false){  return json(array('code'=>0,'msg'=>'添加失败,请选择状态'));}
            if (!TModeL::create($data)) {
                return json(array('code'=>0,'msg'=>'添加失败'));
            }
            return json(array('code'=>200,'msg'=>'添加成功'));
        }

        return view('add');
    }

    function edit(){
        if(request()->isPost()){
            $data = $this->request->post();
            if (!TModeL::update($data)) {
                return json(array('code'=>0,'msg'=>'修改失败'));
            }
            return json(array('code'=>200,'msg'=>'修改成功'));
        }

        $info = TModeL::where('theme_id', input('id'))->find()->toArray();
        $info['img'] = str_replace("/theme",config('aliyun_oss.url')."theme",$info['img']);
        return view('edit',['list'=>$info]);
    }


    function del(){
        $id = input('param.id');
        if (!TModeL::destroy(['theme_id' => $id])) {
            return json(array('code'=>0,'msg'=>'删除失败'));
        }
        db('theme_goods')->where("id = {$id}")->delete();
        return json(array('code'=>200,'msg'=>'删除成功'));
    }

    function index2(){
        if ($this->request->isAjax()) {
            $where['t.theme_id'] = input('id');
            $list = Db::table('je_theme_goods')->alias('t')->join('je_goods g','t.goods_id = g.goods_id')->join('je_store s','g.store_id = s.store_id')->field('s.name as sname,g.name,g.thumbnail_img as img,g.large,t.*')->where($where)->order('t.sort desc ,t.id desc')->paginate(input('limit'))->toArray();
            foreach ($list['data'] as $key=>$itme){
                $label = db('goods_label')->where("goods_id = {$itme['goods_id']}")->value('label_id');
                $label = explode(',',$label);
                foreach ($label as $k=>$v){
                    $name[] = db('label')->where("label_id = {$v}")->value('name');
                }
                $list['data'][$key]['label'] = implode(',',$name);  unset($name);
                $list['data'][$key]['img'] = str_replace("/goods",config('aliyun_oss.url')."goods",$itme['img']);
            }
            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        $info = TModeL::where('theme_id', input('id'))->find()->toArray();
        $info['img'] = str_replace("/theme",config('aliyun_oss.url')."theme",$info['img']);
        return view('list2',['list'=>$info]);
    }


    function add2(){
        if(request()->isPost()){
            $data = $this->request->post();
            Db::startTrans();
            try{
                foreach ($data['id'] as $key=>$itme){
                    $themeGoods['goods_id'] = $itme;
                    $themeGoods['theme_id'] = $data['theme_id'];
                    $theme_goods = db('theme_goods')->where($themeGoods)->find();
                    if(!$theme_goods){
                        db('theme_goods')->insert($themeGoods);
                    }
                }
                Db::commit();
                return json(array('code'=>200,'msg'=>'添加成功'));
            }catch(\Exception $e){
                Db::rollback();
                return json_encode(['code'=>0,'msg'=>'添加失败'.$e->getMessage()]);
            }

        }
    }



    function del2(){
        $id = input('param.id');
        if (!db('theme_goods')->where("id = {$id}")->delete()) {
            return json(array('code'=>0,'msg'=>'删除失败'));
        }
        return json(array('code'=>200,'msg'=>'删除成功'));
    }
}